package com.zzl.bonds.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.bonds.ticket.dao.EnterpriseCreditDataDao;
import com.zzl.bonds.ticket.dto.EnterpriseCredidataAddDto;
import com.zzl.bonds.ticket.dto.EnterpriseCredidataUpdateDto;
import com.zzl.bonds.ticket.entity.EnterpriseCreditDataEntity;
import com.zzl.bonds.ticket.service.EnterpriseCreditDataService;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.User;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 核心企业授信资料表
 *
 * @author gull
 * @date 2022-07-06 13:43:52
 */
@Service("enterpriseCredidataService")
public class EnterpriseCreditDataServiceImpl extends ServiceImpl<EnterpriseCreditDataDao, EnterpriseCreditDataEntity> implements EnterpriseCreditDataService {

    /**
    * 分页查询
    *
    * @param params 查询参数
    * @return 分页数据
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<EnterpriseCreditDataEntity> page = this.page(
                new Query<EnterpriseCreditDataEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增
     *
     * @param enterpriseCredidataAddDto 新增实体
     * @param user 当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void saveEnterpriseCredidata(EnterpriseCredidataAddDto enterpriseCredidataAddDto, User user) throws SysException {
        EnterpriseCreditDataEntity enterpriseCreditDataEntity = new EnterpriseCreditDataEntity();
        BeanUtils.copyProperties(enterpriseCredidataAddDto, enterpriseCreditDataEntity);
        enterpriseCreditDataEntity.setCreatorId(user.getId());
        if(!this.save(enterpriseCreditDataEntity)){
            throw new SysException("A00001", "保存失败！");
        }
    }

    /**
     * 修改
     *
     * @param enterpriseCredidataUpdateDto 修改实体
     * @param user 当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void updateEnterpriseCredidataById(EnterpriseCredidataUpdateDto enterpriseCredidataUpdateDto, User user) throws SysException {
        EnterpriseCreditDataEntity enterpriseCreditDataEntity = new EnterpriseCreditDataEntity();
        BeanUtils.copyProperties(enterpriseCredidataUpdateDto, enterpriseCreditDataEntity);
        enterpriseCreditDataEntity.setUpdaterId(user.getId());
        if(!this.updateById(enterpriseCreditDataEntity)){
            throw new SysException("A00002", "修改失败！");
        }
    }

}